/**
 * @开发版权 
 * @项目名称 VFCloud-PaaS
 * @版本信息 v1.0
 * @开发人员 zhous
 * @开发日期 2023-02-21
 * @修订日期
 * @描述  ApplicationReadyEventListener
 */
package com.vf.cloud.paas.common.listener;

import java.io.File;
import java.util.List;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.jfinal.kit.StrKit;
import com.vf.cloud.paas.app.agent.common.client.RenderClient;
import com.vf.cloud.paas.app.agent.common.domain.Project;
import com.vf.cloud.paas.app.agent.common.server.signalling.player.PlayerServer;
import com.vf.cloud.paas.app.agent.common.server.signalling.streamer.StreamerServer;
import com.vf.cloud.paas.app.agent.common.server.stun.StunServer;
import com.vf.cloud.paas.app.agent.common.server.turn.TurnServer;
import com.vf.cloud.paas.app.master.common.domain.User;
import com.vf.cloud.paas.app.master.common.server.agentManager.AgentManagerServer;
import com.vf.cloud.paas.common.cirrus.Cirrus;
import com.vf.cloud.paas.common.util.EncryptUtil;
import com.vf.cloud.paas.common.util.SpringContextHolder;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Component
public class ApplicationReadyEventListener implements ApplicationListener<ApplicationReadyEvent> {

	@Value("${agent.token}")
	private String agentToken;

	@Value("${agent.enabled}")
	private boolean agentEnabled;

	@Value("${master.enabled}")
	private boolean masterEnabled;

	@Value("${agent.aws.enabled}")
	private boolean enabled;

	@Value("${mysql.port}")
	private int mysqlPort;

	@Value("${app.install.path}")
	private String installPath;

	@Value("${agent.project.path}")
	private String projectPath;

	@Override
	public void onApplicationEvent(ApplicationReadyEvent contextReadyEvent) {

		if (agentEnabled) {

			try {
				List<Project> projects = Project.dao.findAll();

				for (Project project : projects) {
					File f = new File(String.format("%s%s", projectPath, project.getExeRelativePath()));
					if (f.exists()) {
						Process process = Runtime.getRuntime()
								.exec("cmd /c start /b taskkill /f /t /im Superapi.exe" + f.getName());
						process.wait();
						process.destroy();
					} else {
						project.setState("-1");
						project.update();
					}
				}
			} catch (Exception e) {
			}

			if (StrKit.isBlank(agentToken)) {
				log.info(String.format("AgentToken is null .", "failed"));
				return;
			}

			Cirrus.agentToken = agentToken;
			log.info(String.format("AgentToken>> %s", agentToken));

			Cirrus.renderClient = SpringContextHolder.getBean(RenderClient.class);
			Cirrus.renderClient.run();
			if (enabled) {
				Cirrus.Stun_Server = SpringContextHolder.getBean(StunServer.class);
				Cirrus.Stun_Server.run();

				Cirrus.Turn_Server = SpringContextHolder.getBean(TurnServer.class);
				Cirrus.Turn_Server.run();
			}

			Cirrus.Streamer_Server = SpringContextHolder.getBean(StreamerServer.class);
			Cirrus.Streamer_Server.run();

			Cirrus.Player_Server = SpringContextHolder.getBean(PlayerServer.class);
			Cirrus.Player_Server.run();

		}

		if (masterEnabled) {

			User user = User.dao.findById("1");
			if (user == null) {
				user = new User();
				user.setId("1");
				user.setPid("-1");
				user.setName("超级管理员");
				user.setUsername("administrator");
				user.setPassword(EncryptUtil.encrypt("administrator"));
				user.setRoleMark("admin");
				user.setIsLimit("-1");
				user.save();
			}

//			AssetClassify sysFolder = AssetClassify.dao.findById("sys");
//			if (sysFolder == null) {
//				sysFolder = new AssetClassify();
//				sysFolder.setId("sys");
//				sysFolder.setName("系统资产");
//				sysFolder.setPid("-1");
//				sysFolder.setCode("sys");
//				sysFolder.setSort(-1);
//				sysFolder.setAllowRemove("-1");
//				sysFolder.setType("folder");
//				sysFolder.save();
//
//				AssetClassify defaultModelFolder = AssetClassify.dao.findById("sys_model");
//				if (defaultModelFolder == null) {
//					defaultModelFolder = new AssetClassify();
//					defaultModelFolder.setId("sys_model");
//					defaultModelFolder.setPid("sys");
//					defaultModelFolder.setName("模型");
//					defaultModelFolder.setCode("sys_model");
//					defaultModelFolder.setSort(-1);
//					defaultModelFolder.setAllowRemove("-1");
//					defaultModelFolder.setType("model");
//					defaultModelFolder.save();
//
//				}
//				AssetClassify defaultIconFolder = AssetClassify.dao.findById("sys_icon");
//				if (defaultIconFolder == null) {
//					defaultIconFolder = new AssetClassify();
//					defaultIconFolder.setId("sys_icon");
//					defaultIconFolder.setPid("sys");
//					defaultIconFolder.setName("图标");
//					defaultIconFolder.setCode("sys_icon");
//					defaultIconFolder.setSort(0);
//					defaultIconFolder.setAllowRemove("-1");
//					defaultIconFolder.setType("model");
//					defaultIconFolder.save();
//				}
//			}

//			AssetClassify userFolder = AssetClassify.dao.findById("user");
//			if (userFolder == null) {
//				userFolder = new AssetClassify();
//				userFolder.setId("user");
//				userFolder.setName("用户资产");
//				userFolder.setPid("-1");
//				userFolder.setCode("user");
//				userFolder.setSort(0);
//				userFolder.setAllowRemove("-1");
//				userFolder.setType("folder");
//				userFolder.save();
//
//				AssetClassify userModelFolder = AssetClassify.dao.findById("user_model");
//				if (userModelFolder == null) {
//					userModelFolder = new AssetClassify();
//					userModelFolder.setId("user_model");
//					userModelFolder.setPid("user");
//					userModelFolder.setName("模型");
//					userModelFolder.setCode("user_model");
//					userModelFolder.setSort(-1);
//					userModelFolder.setAllowRemove("-1");
//					userModelFolder.setType("model");
//					userModelFolder.save();
//
//				}
//
//				AssetClassify userIconFolder = AssetClassify.dao.findById("user_icon");
//				if (userIconFolder == null) {
//					userIconFolder = new AssetClassify();
//					userIconFolder.setId("user_icon");
//					userIconFolder.setPid("user");
//					userIconFolder.setName("图标");
//					userIconFolder.setCode("user_icon");
//					userIconFolder.setSort(0);
//					userIconFolder.setAllowRemove("-1");
//					userIconFolder.setType("icon");
//					userIconFolder.save();
//				}
//				}

			Cirrus.agentManagerServer = SpringContextHolder.getBean(AgentManagerServer.class);
			Cirrus.agentManagerServer.run();
		}

	}

}
